// pages/seckill/seckill.js
const app = getApp()
var url = require('../../utils/url.js');
var page = 1;
var last_page = 0;
var type=1;
var timeData;
var immediately;
var leftTime;
var start;
var endTime;
var nextTime;
var user_id;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl3 + 'goods/limit/goods',
      data: { page: page, type: type },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(that.data.shopList)
        wx.hideLoading();
        var shopList = res.data.data.data
        if (type == 1) {
          var bill = "bill"
          for (var i = 0; i < shopList.length; i++) {
            var billF = (shopList[i].sales) / (shopList[i].stronge + shopList[i].sales) * 100;
            shopList[i].bill = billF.toFixed(2) + "%"
          }
          that.setData({
            shopList: shopList
          })
        } else {
          that.setData({
            shopList: shopList
          })
        }
        page++;
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,  //小点
    indicatorColor: "white",
    autoplay: true,  //是否自动轮播
    interval: 3000,  //间隔时间
    duration: 500,  //滑动时间
    indicatorActiveColor: "#DD2626",
    banner: [],
    type:1,
    d: "",
    h:"",
    m: "",
    s: "",
    d1: "",
    h1: "",
    m1: "",
    s1: "",
    endDate2: '2019:5:1 00:50:00',
    shopList:[],
    immediately:"",
    start:"",
    queS:true,
    first:"",
    fast_begin:"",
    title:'',
    countdown:false,
    countdown1:false,
  },
  countTime1() {
    var that = this;
    var date = new Date();
    var now = date.getTime();
    var endDate = new Date(nextTime);//设置截止时间
    // console.log(endDate)
    var end = endDate.getTime();
    var leftTime = end - now; //时间差    
    // console.log(leftTime)                          
    var d, h, m, s, ms;
    if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24)+d*24;
      m = Math.floor(leftTime / 1000 / 60 % 60);
      s = Math.floor(leftTime / 1000 % 60);
      ms = Math.floor(leftTime % 1000);
      ms = ms < 100 ? "0" + ms : ms
      s = s < 10 ? "0" + s : s
      m = m < 10 ? "0" + m : m
      h = h < 10 ? "0" + h : h
      // that.setData({
      //   countdown: d + "：" + h + "：" + m + "：" + s + ":" + ms,
      // })
      that.setData({
        d1: d,
        h1: h,
        m1: m,
        s1: s,
      })
      //递归每秒调用countTime方法，显示动态时间效果
      setTimeout(that.countTime1, 100);
    } else {
      console.log('')
      that.setData({
        countdown1: true,
      })
    }
  },
  countTime() {
    var that = this;
    var date = new Date();
    var now = date.getTime();
    var endDate = new Date(endTime);//设置截止时间
    // console.log(endDate)
    var end = endDate.getTime();
    var leftTime = end - now; //时间差    
    // console.log(leftTime)                          
    var d, h, m, s, ms;
    if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24)+d*24;
      m = Math.floor(leftTime / 1000 / 60 % 60);
      s = Math.floor(leftTime / 1000 % 60);
      ms = Math.floor(leftTime % 1000);
      ms = ms < 100 ? "0" + ms : ms
      s = s < 10 ? "0" + s : s
      m = m < 10 ? "0" + m : m
      h = h < 10 ? "0" + h : h
      // that.setData({
      //   countdown: d + "：" + h + "：" + m + "：" + s + ":" + ms,
      // })
      that.setData({
        d: d,
        h: h,
        m: m,
        s: s
      })
      //递归每秒调用countTime方法，显示动态时间效果
      setTimeout(that.countTime, 100);
    } else {
      console.log('已截止')
      that.setData({
        countdown: true
      })
    }
  },
  getTime(){
    var that = this;
    wx.request({
      url: url.URl3 + 'goods/limit/immediately',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg;
        if (res.data.code == 200) {
          if(res.data.data.first){
            endTime = res.data.data.first.replace(/-/g, '/');
            that.countTime()
          }
          if (res.data.data.fast_begin){
            nextTime = res.data.data.fast_begin.replace(/-/g, '/')
            that.countTime1()
          }
          that.setData({
            endTime: endTime,
            fast_begin: res.data.data.fast_begin,
            first: res.data.data.first,
            title:res.data.data.title
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
        
        
        
      }
    })
  },
  onLoad: function (options) {
    var that = this;
    console.log(options)
    that.getList(type)
    that.getBanner()
    that.getTime()
    if (options.userId) {
      wx.setStorage({
        key: 'rec_id',
        data: options.userId,
      })
    }
  },
  makeDurationToSeconds(time){
    var str = time;
    var arr = str.split(':');
    var hs = parseInt(arr[0] * 3600);
    var ms = parseInt(arr[1] * 60);
    var ss = parseInt(arr[2]);
    var seconds = hs + ms + ss;
    return seconds;
  },
  selSeckill:function(){
     var that=this;
     page=1;
     var type=that.data.type;
     if(type==1){
       type=2;
       that.setData({
         type:2
       })
       that.getList(type)
     } else {
       type = 1;
       that.setData({
         type:1
       })
       that.getList(type)
     }
  },
  getList:function(type){
    var that=this;
    that.setData({
      queS: true
    })
    wx.showLoading({
      title: '加载中..',
    })
    that.setData({
      shopList:[]
    })
    wx.request({
      url: url.URl3 + 'goods/limit/goods',
      data: { page: page,type:type},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg;
        wx.hideLoading()
        if (res.data.code == 200) {
          last_page=res.data.data.last_page
          var shopList = res.data.data.data
          // console.log(shopList,1111)
           if(type==1){
            
             var bill = "bill"
             for(var i=0;i<shopList.length;i++){
               var billF = (shopList[i].sales) / (shopList[i].stronge + shopList[i].sales) * 100;
               shopList[i].bill = billF.toFixed(2) + "%";
             }
             that.setData({
               shopList: shopList
             })
           }else{
             that.setData({
               shopList: shopList
             })
           }
        } else if(res.data.code==201){
            that.setData({
               queS:false,
              shopList:null,
              //  type:2,
            })
            // type=2
          // that.getList(type)
        } else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getBanner(){
    var that=this;
    wx.request({
      url: url.URl3 + 'goods/limit/ads',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg;
        wx.hideLoading()
        if (res.data.code == 200) {
          that.setData({
            banner: res.data.data
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     wx.getStorage({
       key: 'userinfo',
       success: function(res) {
         user_id=res.data.id
       },
     })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    type = 1;
    page = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },
      
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
   
    var that = this;
    var row = that.data.row;
    if (res.from === 'button') {

    }
    return {
      title:this.data.title,
      path: '/pages/seckill/seckill?userId=' + user_id,
      success: function (res) {
        console.log('成功', res)
      }
    }
  }
})